package com.wu.util.spider.service.impl;

import com.wu.util.spider.dao.MovieDao;
import com.wu.util.spider.entity.RecordBaseEntity;
import com.wu.util.spider.service.AsyncDisposeService;
import com.wu.util.spider.service.BaseSaveWorkerService;
import com.wu.util.spider.util.Constant;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

/**
 * @author 50816
 */
@Service("xiGuaDisposeServiceImpl")
public class XiGuaDisposeServiceImpl extends BaseSaveWorkerService implements AsyncDisposeService {

    Logger logger = LoggerFactory.getLogger(XiGuaDisposeServiceImpl.class);

    @Autowired
    MovieDao movieDao;

    @Async("taskExecutor")
    @Override
    public void saveImage(String path, String fileName) {
        try {
            URL url = new URL(path);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestProperty("User-agent", Constant.UserAgent.getValue());
            InputStream inputStream = con.getInputStream();
            File file = new File(Constant.SaveBasePath.getValue() + "/" + fileName);
            baseSaveImage(inputStream,file);
            logger.info("图片:{}写入成功",fileName);
        } catch (IOException e) {
            logger.error("保存图片:{}失败:{}", fileName, e.getMessage());
        }
    }

    @Override
    public void saveToText(List<RecordBaseEntity> list, String saveType) {

    }

    @Override
    public void saveToDatabase(List<RecordBaseEntity> list) {

    }

    @Async("taskExecutor")
    @Override
    public void resolvingDetail(String document, RecordBaseEntity recordBaseEntity) {
        Element body = Jsoup.parse(document).body();
        Elements main = body.select(".myui-content__detail");
        String title = main.select(".title").text();
        String videoQuality = main.select(".title").select("font").text();
        String score = main.select(".score").select(".branch").text();
        Element movieInfo = main.select(".data").get(0);
        String videoType = movieInfo.select("a").get(0).text();
        String site = movieInfo.select("a").get(1).text();
        String year = movieInfo.select("a").get(2).text();
        Elements actors = main.select(".data").get(1).select("a");
        StringBuffer buffer = new StringBuffer();
        for (Element actor : actors) {
            buffer.append(actor.text()).append(",");
        }
        String allActors = buffer.toString().substring(0, buffer.toString().length() - 1);
        String director = main.select(".data").get(2).select("a").text();
        String desc = main.select(".desc").select(".data").text();
        Elements image = body.select(".myui-content__thumb").select("a");
        String imgSrc = image.attr("data-original");
        logger.info("电影名:{},清晰度:{},评分:{},分类:{},地区:{},年份:{},主演:{},导演:{},简介:{},",
                title, videoQuality, score, videoType, site, year, allActors, director, desc);
        recordBaseEntity.setTitle(title);
        recordBaseEntity.setDefinition(videoQuality);
        recordBaseEntity.setScore(score);
        recordBaseEntity.setResourceType(videoType);
        recordBaseEntity.setSite(site);
        recordBaseEntity.setYear(year);
        recordBaseEntity.setActor(allActors);
        recordBaseEntity.setDirector(director);
        recordBaseEntity.setDesc(desc);
        movieDao.saveMovieResource(recordBaseEntity);
    }
}
